package com.hitqz.robot.biz.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hitqz.robot.api.business.dto.PatrolContentExcelDto;
import com.hitqz.robot.api.business.model.vo.patrol.PatrolPosPoseGroupVo;
import com.hitqz.robot.api.common.entity.PatrolDev;
import com.hitqz.robot.api.common.entity.PatrolItem;
import com.hitqz.robot.api.common.entity.PatrolPosPoseGroup;
import com.hitqz.robot.api.common.entity.PatrolPoseGroup;
import com.hitqz.robot.biz.service.PatrolDevService;
import com.hitqz.robot.biz.service.PatrolItemService;
import com.hitqz.robot.biz.service.PatrolPosPoseGroupService;
import com.hitqz.robot.biz.service.PatrolPoseGroupService;
import com.hitqz.robot.common.core.util.R;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wengyx
 * @date 2024/8/7
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/patrolDev")
@Tag(name = "巡检设备管理")
public class PatrolDevController {

    private final PatrolDevService patrolDevService;

    private final PatrolPosPoseGroupService patrolPosPoseGroupService;

    private final PatrolPoseGroupService patrolPoseGroupService;

    private final PatrolItemService patrolItemService;

    @PostMapping("/tree")
    @Operation(summary = "获取巡检设备树")
    public R selectTree() {
        return R.ok(patrolDevService.selectTree());
    }

    @GetMapping("/{id}" )
    @Operation(summary = "根据id查询巡检设备")
    public R<PatrolDev> getById(@PathVariable("id") Integer id) {
        PatrolDev patrolDev = patrolDevService.getById(id);
        return R.ok(patrolDev);
    }

    @GetMapping("/{id}/posPoseGroup" )
    @Operation(summary = "根据id查询巡检设备底下位置姿态组关系")
    public R<List<PatrolPosPoseGroupVo>> getPosPoseGroupListById(@PathVariable("id") Integer id) {
        List<PatrolPosPoseGroupVo> voList = patrolPosPoseGroupService.getVoListByDevId(id);
        return R.ok(voList);
    }



    @PostMapping
    @Operation(summary = "新增巡检设备")
    public R save(@RequestBody @Validated PatrolDev dto) {
        return R.ok(patrolDevService.save(dto));
    }

    @PutMapping
    @Operation(summary = "修改巡检设备")
    public R updateById(@RequestBody @Validated PatrolDev dto) {
        return R.ok(patrolDevService.updateById(dto));
    }

    @DeleteMapping
    @Operation(summary = "根据id删除巡检设备")
    public R removeById(@RequestBody Integer[] ids) {
        return R.ok(patrolDevService.removeBatchByIds(CollUtil.toList(ids)));
    }

    //todo  下载模板




    //fixme  导入Excel数据
    @PostMapping("/excel/upload")
    @Operation(summary = "新增巡检设备")
    public R upload() {
        File excelFile = FileUtil.file("/Users/wengyongxin/Downloads/PO罐区巡检点.xlsx");
        if (excelFile.exists()){
            ArrayList<PatrolContentExcelDto> list = new ArrayList<>();
            EasyExcel.read(excelFile, PatrolContentExcelDto.class, new PageReadListener<PatrolContentExcelDto>(dataList -> {
                for (PatrolContentExcelDto demoData : dataList) {
                    log.info("读取到一条数据{}", JSON.toJSONString(demoData));
                    list.add(demoData);
                }
            })).sheet().doRead();
            for (PatrolContentExcelDto dto : list) {
                String roomName = dto.getRoomName();
                String devName = dto.getDevName();
                String itemName = dto.getItemName();
                PatrolDev room = patrolDevService.getOne(Wrappers.<PatrolDev>lambdaQuery().eq(PatrolDev::getName, roomName).eq(PatrolDev::getIsParent, 1).last("limit 1"));
                if (room==null){
                    room = new PatrolDev();
                    room.setIsParent(1);
                    room.setParentId(0);
                    room.setName(roomName);
                    log.info("save room {},{},{}",patrolDevService.save(room),room.getId(),room);
                }
                PatrolDev dev = patrolDevService.getOne(Wrappers.<PatrolDev>lambdaQuery().eq(PatrolDev::getName, devName).eq(PatrolDev::getParentId, room.getId()).last("limit 1"));
                if (dev==null){
                    dev = new PatrolDev();
                    dev.setParentId(room.getId().intValue());
                    dev.setName(devName);
                    dev.setIsParent(0);
                    log.info("save dev:{},{},{}",patrolDevService.save(dev),dev.getId(),dev);
                }
                PatrolPoseGroup patrolPoseGroup = patrolPoseGroupService.getOne(Wrappers.<PatrolPoseGroup>lambdaQuery().eq(PatrolPoseGroup::getName, itemName).last("limit 1"));
                if (patrolPoseGroup==null){
                    patrolPoseGroup = new PatrolPoseGroup();
                    patrolPoseGroup.setName(itemName);
                    log.info("save patrolPoseGroup:{},{},{}",patrolPoseGroupService.save(patrolPoseGroup),patrolPoseGroup.getId(),patrolPoseGroup);
                }
                PatrolPosPoseGroup posPoseGroup = patrolPosPoseGroupService.getOne(Wrappers.<PatrolPosPoseGroup>lambdaQuery().eq(PatrolPosPoseGroup::getDevId, dev.getId())
                        .eq(PatrolPosPoseGroup::getPatrolPoseGroupId, patrolPoseGroup.getId()).last("limit 1"));
                if (posPoseGroup==null){
                    posPoseGroup = new PatrolPosPoseGroup();
                    posPoseGroup.setDevId(dev.getId().intValue());
                    posPoseGroup.setPatrolPoseGroupId(patrolPoseGroup.getId().intValue());
                    posPoseGroup.setIsBack(0);
                    posPoseGroup.setIsDynamic(0);
                    posPoseGroup.setIsLight(0);
                    log.info("save posPoseGroup:{},{},{}",patrolPosPoseGroupService.save(posPoseGroup),posPoseGroup.getId(),posPoseGroup);
                }
                PatrolItem patrolItem = patrolItemService.getOne(Wrappers.<PatrolItem>lambdaQuery().eq(PatrolItem::getPatrolPosPoseGroupId, posPoseGroup.getId())
                        .eq(PatrolItem::getName, itemName).last("limit 1"));
                if (patrolItem==null){
                    patrolItem = new PatrolItem();
                    patrolItem.setName(itemName);
                    patrolItem.setPatrolPosPoseGroupId(posPoseGroup.getId().intValue());
                    log.info("save item:{},{},{}",patrolItemService.save(patrolItem),patrolItem.getId(),patrolItem);
                }
                //log.info("dto:{}",dto);
            }

        }
        return R.ok();
    }






}
